<template>
    <app-layout>
        <view class="all">
            <view class="goods-head-box" v-if="goods">
                <app-goods-banner :pic-list="goods.pic_url" :share="goods.share"
                                  :goods_id="lottery_id"
                                  sign="lottery"
                                  :video-url="goods.video_url"></app-goods-banner>
                <app-service :list="goods.services"></app-service>
                <view class="time main-center">
                    <view v-if="user_log === null || user_log.status <= 1"
                          class="time-info dir-left-nowrap cross-center">
                        <text class="icon"></text>
                        <view v-if="new_status == 2">距活动开始：</view>
                        <view v-else>距活动结束：</view>
                        <text v-if="times.day>0 || times.hour>0">{{times.day}}天{{times.hour}}小时</text>
                        <text v-if="times.day==0 && times.hour==0">{{times.minute}}分{{times.second}}秒</text>
                    </view>
                    <view v-else class="time-info time-error dir-left-nowrap cross-center">
                        <text class="icon"></text>
                        <view>活动已结束</view>
                    </view>
                </view>
            </view>
            <view class="goods" v-if="goods">
                <view class="t-omit-three name">{{goods.name}}</view>
                <view class="dir-left-nowrap">
                    <view class="detail box-grow-1">
                        <view class="dir-left-nowrap box-grow-0">
                            <text>共</text>
                            <text class="bold">{{lottery.stock}}</text>
                            <text>份</text>
                            <view class="num">{{goods.num}}人参与</view>
                        </view>
                        <view class="price dir-left-nowrap box-grow-0">
                            <view class="box-grow-0">￥0</view>
                            <view class="box-grow-0 del" v-if="isUnderlinePrice">售价￥{{goods.original_price}}</view>
                        </view>
                    </view>
                    <view @click="shareClick" class="share box-grow-0 cross-center dir-top-nowrap">
                        <text class="icon"></text>
                        <view class="t-mini t-extra-small-color share">分享</view>
                    </view>
                    <view>
                        <app-share-qr-code :url="poster + `&goods_id=` + goods.id"
                                           v-model="shareShow"></app-share-qr-code>
                    </view>
                </view>
            </view>

            <!-- 规格信息 -->
            <view class="lottery-attr">
                <view class="box cross-center">
                    <view class="border box-grow-0">商品信息</view>
                    <view class="text dir-left-wrap t-omit">
                        <block v-for="(value,key) in goods.attr_list" :key="key">
                            <text>{{value.attr_group_name}}：{{value.attr_name}}</text>
                        </block>
                    </view>
                </view>
            </view>

            <view @click="qrcode" class="lottery-share" v-if="setting.cs_status ===1 && setting.cs_prompt_pic">
                <image :src="setting.cs_prompt_pic" mode="aspectFit" load-lazy></image>
            </view>

            <view class="step cross-center dir-left-nowrap">
                <view class="box-grow-1">抽奖流程</view>
                <view @click="rule" class="cross-center">
                    <text class="icon-right box-grow-0"></text>
                </view>
            </view>
            <view class="step-info dir-left-nowrap">
                <block v-for="(v,k) in rule_list" :key="k">
                    <view class="box-grow-1 dir-left-nowrap main-center cross-center">
                        <view class="label">{{v.key}}</view>
                        <text class="value">{{v.value}}</text>
                    </view>
                </block>
            </view>
            <view v-if="lucky_list" class="dir-top-nowrap lucky-all">
                <view class='box-grow-1'>实时幸运码</view>
                <view class='box-grow-1 friend'>邀请好友参与，额外获赠幸运码，提高中奖率哦！</view>
                <swiper v-if="lucky_list.length" class='swiper' :class="[lucky_list.length > 1?'':'swiper-one']"
                        :display-multiple-items="lucky_list.length > 1 ? 2: 1" vertical="true"
                        autoplay="true"
                        interval="2000" duration="500" circular="true">
                    <block v-for="(v,k) in lucky_list" :key="k">
                        <swiper-item style="height: 50%" catchtouchmove='catchTouchMove'>
                            <view class="dir-left cross-center lucky-code">
                                <image class="box-grow-0" :src="v.user.userInfo.avatar" load-lazy></image>
                                <view class="box-grow-1 t-omit nickname">{{v.user.nickname}}</view>
                                <view class="box-grow-0 font-normal">{{v.lucky_num}}个幸运码</view>
                            </view>
                        </swiper-item>
                    </block>
                </swiper>
            </view>
            <view class="app-goods-marketing">
                <app-goods-marketing :express="goods.express"></app-goods-marketing>
            </view>
            <view class="goods-rule">商品详情</view>
            <view class="rule-info">
                <app-rich-text :content="goods.detail"></app-rich-text>
            </view>
	        <view>
		        <app-empty-bottom :height="Number(100)"></app-empty-bottom>
	        </view>
	        <view>
		        <app-iphone-x>
                    <view class='bottom main-center cross-center' slot="empty-area">
                        <view class='bottom-first main-center cross-center' @click='buyNow'>直接购买</view>

                        <view v-if="new_status == 2" class="bottom-end main-center cross-center">尚未开始
                        </view>
                        <view v-else-if="lottery.status == 0" class="bottom-end main-center cross-center">已关闭</view>
                        <view v-else-if="user_log == null" class="bottom-second main-center cross-center"
                              @click='subscribe'>立即抽奖
                        </view>
                        <view v-else-if="user_log.status <= 1" class="bottom-end main-center cross-center">已参与</view>
                        <view v-else class="bottom-end main-center cross-center">已结束</view>
                    </view>
		        </app-iphone-x>
	        </view>
        </view>
        <view v-if="award_bg" class="main-center cross-center animation-model">
            <view @click="prize" class="main-center detaill">
                <view class="ani_box" :animation="animation_trans_lottery">
                    <image :src="appImg.lottery_box"></image>
                </view>
            </view>
            <image class="boxbg" :src="appImg.lottery_boxbg"></image>
        </view>
    </app-layout>
</template>

<script>
    import {mapState} from 'vuex';
    import appQuickNavigation from "../../../components/page-component/app-quick-navigation/app-quick-navigation";
    import appRichText from "../../../components/basic-component/app-rich/parse.vue";
    import appGoodsBanner from "../../../components/page-component/goods/app-goods-banner.vue";
    import appService from "../../../components/page-component/goods/app-goods-service.vue";
    import appShareQrCode
    from '../../../components/page-component/app-share-qr-code-poster/app-share-qr-code-poster.vue';
    import appIphoneX from '../../../components/basic-component/app-iphone-x/app-iphone-x.vue';
    import appEmptyBottom from '../../../components/basic-component/app-empty-bottom/app-empty-bottom.vue';
    import appGoodsMarketing from '../../../components/page-component/goods/app-goods-marketing.vue';

    export default {
        name: "goods",
        components: {appQuickNavigation, appRichText,appGoodsBanner, appService, appShareQrCode,appIphoneX, appEmptyBottom, appGoodsMarketing},
        computed: {
            ...mapState({
                appImg: state => state.mallConfig.plugin.lottery.app_image,
                isUnderlinePrice: state => state.mallConfig.mall.setting.is_underline_price,
            })
        },
        data() {
            let integral_time, integral_animation;
            return {
                integral_time,
                integral_animation,
                animation_trans_lottery: null,
                rule_list: [
                    {key: 1, value: "参与\n抽奖"},
                    {key: 2, value: "等待\n开奖"},
                    {key: 3, value: "幸运\n中奖"},
                    {key: 4, value: "下单\n收礼"}
                ],

                times: {},
                poster: this.$api.lottery.poster,
                user_log: null,
                lottery: null,
                goods: '',
                lucky_list: null,
                lottery_id: '',
                award_bg: '',
                shareShow: false,
                setting: '',
				lottery:'',
                new_status: 0,
                template_message: null,
				user_log:''
            }
        },

        onHide() {
            clearInterval(this.integral_time)
        },
        onUnload() {
            clearInterval(this.integral_time);
        },

        //
        onShareAppMessage: function (e) {
            return this.$shareAppMessage({
                title: this.goods.name,
                path: '/plugins/lottery/goods/goods',
                params: {
                    lottery_id: this.lottery.id,
                }
            });
        },

        onLoad(options) {
            const self = this;

            self.lottery_id = options.lottery_id;
            self.getSetting();
            self.clerk(options.user_id);

            self.$showLoading({title: '加载中'});
            self.$request({
                url: self.$api.lottery.goods,
                data: {
                    lottery_id: self.lottery_id
                }
            }).then(info => {
                self.$hideLoading();
                if (info.code === 0) {
                    [
                        self.goods,
                        self.lottery,
                        self.lucky_list,
                        self.user_log,
                        self.new_status,
                        self.template_message,
                    ] = [
                        info.data.goods,
                        info.data.lottery,
                        info.data.lucky_list,
                        info.data.user_log,
                        info.data.new_status,
                        info.data.template_message,
                    ];
                    self.setTimeStart(self.new_status == 2 ? self.lottery.start_at : self.lottery.end_at);
                } else {
                    uni.showToast({icon: 'none', title: info.msg});
                }
            }).catch(info => {
                self.$hideLoading();
            });
        },
        methods: {
            qrcode() {
                uni.navigateTo({url: `/plugins/lottery/qrcode/qrcode`})
            },
            prize() {
                uni.navigateTo({url: `/plugins/lottery/detail/detail?lottery_id=` + this.lottery.id});
                clearInterval(this.lotteryInter);
                this.award_bg = false;
            },
            rule() {
                uni.navigateTo({url: `/plugins/lottery/rule/rule?rules=` + this.setting.rule});
            },
            catchTouchMove() {
                return false;
            },
            getSetting: function () {
                const self = this;
                self.$request({
                    url: self.$api.lottery.setting,
                }).then(info => {
                    if (info.code === 0) {
                        self.setting = info.data.setting;
                    }
                }).catch(e => {
                    // 网络请求出错
                    console.log(e);
                })
            },

            clerk(user_id) {
                const self = this;
                if (!user_id) return

                self.$request({
                    url: self.$api.lottery.clerk,
                    data: {
                        user_id,
                        lottery_id: self.lottery_id,
                    },
                    method: 'POST',
                }).then(info => {
                    console.log(info);
                })
            },

            setTimeStart(info) {
                const self = this;
                clearInterval(self.integral_time);
                let func = function (info) {
                    let time = info.replace(/-/g, '/');
                    let diff_time = parseInt((new Date(time).getTime() - new Date().getTime()) / 1000);
                    let day = 0, hour = 0, minute = 0, second = 0;

                    if (diff_time > 0) {
                        day = Math.floor(diff_time / (60 * 60 * 24));
                        hour = Math.floor(diff_time / (60 * 60)) - (day * 24);
                        minute = Math.floor(diff_time / 60) - (day * 24 * 60) - (hour * 60);
                        second = Math.floor(diff_time) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
                    }

                    self.times = {day, hour, minute, second};
                    if (self.times <= 0) {
                        clearInterval(self.integral_time)
                    }
                };
                func(info);
                self.integral_time = setInterval(() => {
                    func(info)
                }, 1000)
            },
            subscribe: function () {
                if (this.award_bg) return;
                const self = this;
                self.$subscribe(this.template_message).then(res => {
                    let tpl_id = this.template_message[0];
                    if (res[tpl_id] == 'accept') {
                        uni.showModal({
                            title: '提示',
                            content: '订阅成功',
                            showCancel: false,
                            success(res) {
                                self.buyZero();
                            }
                        })
                    } else {
                        uni.showModal({
                            title: '提示',
                            content: '取消订阅',
                            showCancel: false,
                            success(res) {
                                self.buyZero();
                            }
                        })
                    }
                }).catch(res => {
                    self.buyZero();
                });
            },
            buyZero: function () {
                const self = this;
                self.award_bg = !self.award_bg;
                setTimeout(() => {
                    let animation = wx.createAnimation({
                        duration: 500,
                        timingFunction: 'linear',
                        delay: 0,
                        transformOrigin: '50% 50%',
                    })
                    if (self.award_bg) {
                        animation.width('360rpx').height('314rpx').step();
                    } else {
                        animation.scale(0, 0).opacity(0).step();
                    }
                    self.animation_trans_lottery = animation.export();
                    setTimeout(() => {
                        let circleCount = 0;
                        self.integral_animation = setInterval(() => {
                            if (circleCount % 2 === 0) {
                                animation.scale(0.9).opacity(1).step();
                            } else {
                                animation.scale(1).opacity(1).step();
                            }

                            self.animation_trans_lottery = animation.export();
                            circleCount++;
                            if (circleCount === 500) {
                                circleCount = 0;
                            }
                        }, 500);
                    }, 500)
                }, 50)
            },

            detail() {
                clearInterval(this.integral_animation);
                this.award_bg = false
            },

            onImageListClick(e) {
                let urls = [];
                let index = e.currentTarget.dataset.index;
                for (let i in this.goods.pic_url) {
                    urls.push(this.goods.pic_url[i].pic_url);
                }
                uni.previewImage({
                    urls: urls, // 需要预览的图片http链接列表
                    current: urls[index],
                });
            },

            buyNow: function () {
                uni.navigateTo({url: `/pages/goods/goods?id=` + this.lottery.buy_goods_id})
            },

            shareClick() {
                this.shareShow = true;
            }
        }
    }
</script>


<style scoped lang="scss">
    .lottery-attr {
        background: #ffffff;

        .box {
            margin: 0 #{24rpx};
            border-top: 1px solid #e2e2e2;
            padding: #{28rpx} 0;
        }

        .border {
            border: 1px solid #666666;
            border-radius: #{20rpx};
            font-size: #{24rpx};
            line-height: 1;
            padding: #{8rpx} #{16rpx};
            color: #666666;
        }

        .text {
            margin-left: #{24rpx};
            color: #353535;
            font-size: #{26rpx};
            max-width: #{520rpx};

            text:after {
                content: '，'
            }

            text:last-child:after {
                content: ''
            }
        }
    }

    .lottery-share {
        margin: #{20rpx} 0;

        image {
            height: #{140rpx};
            margin: 0 auto;
            width: #{702rpx};
            display: block;
        }
    }

    .t1 {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.25);
    }

    .all {
        //padding-bottom: #{120rpx};
    }

    .goods-head-box {
        position: relative;
    }

    .time {
        position: absolute;
        bottom: #{120rpx};
        width: 100%;

        .time-info {
            height: #{56rpx};
            background: #ff4544;
            opacity: 0.6;
            padding: 0 #{24rpx};
            border-radius: #{28rpx};
            font-size: #{28rpx};
            color: #FFFFFF;

            .icon {
                height: #{24rpx};
                width: #{24rpx};
                background-repeat: no-repeat;
                background-size: 100% 100%;
                background-image: url('./../image/lottery-white.png');
            }

            view {
                margin-left: #{24rpx};
            }
        }

        .time-error {
            background: #000000;
        }
    }

    .goods {
        background: #FFFFFF;
        padding: #{32rpx} #{24rpx} #{24rpx};
        color: #999999;

        .name {
            color: #353535;
            font-size: #{32rpx};
        }

        .detail {
            margin-top: #{24rpx};

            .bold {
                font-weight: bold;
                font-size: #{36rpx};
                color: #ff4544;
            }

            .num {
                margin-right: #{16rpx};
                margin-left: #{12rpx};
            }

            .price {
                color: #ff4544;
                padding-top: #{32rpx};
            }

            .del {
                color: #999999;
                margin-left: #{12rpx};
                text-decoration: line-through;
            }
        }

        .share {
            margin-top: #{24rpx};

            .icon {
                height: #{40rpx};
                width: #{40rpx};
                background-image: url('../../../static/image/icon/icon-share.png');
                background-repeat: no-repeat;
                background-size: 100% 100%;
            }

            view {
                font-size: #{22rpx};
                color: #999999;
                margin-top: #{10rpx};
            }
        }
    }

    .step {
        margin-top: #{20rpx};
        padding: #{24rpx} #{24rpx} 0;
        background: #FFFFFF;

        view {
            font-size: #{32rpx};
            color: #353535;
        }

        .icon-right {
            background-repeat: no-repeat;
            background-size: 100% 100%;
            height: #{22rpx};
            width: #{12rpx};
            margin-left: #{16rpx};
            background-image: url("../../../static/image/icon/arrow-right.png");
        }
    }

    .step-info {
        padding: #{40rpx} 0;
        background: #FFFFFF;

        .label {
            font-size: #{52rpx};
            font-weight: bold;
            color: #dddddd;
            margin-right: #{20rpx};
        }

        .value {
            color: #666666;
        }
    }

    .code {
        margin-top: #{20rpx};
    }

    .lucky-all {
        margin-top: #{20rpx};
        overflow-y: hidden;
        padding: #{24rpx};
        background: #ffffff;
        color: #353535;

        .friend {
            margin-top: #{16rpx};
            font-size: #{24rpx};
            margin-bottom: #{24rpx};
            color: #999999;
        }
        .swiper {
            // #ifdef MP-ALIPAY
            height: #{92rpx};
            // #endif
            // #ifndef MP-ALIPAY
            height: #{184rpx};
            // #endif
            .lucky-code {
                height: #{88rpx};
                background: #fff5f5;
                padding: 0 #{24rpx};
                margin-bottom: #{8rpx};

                image {
                    margin-right: #{32rpx};
                    height: #{64rpx};
                    width: #{64rpx};
                    border-radius: 50%
                }

                .nickname {
                    color: #353535;
                }

                .font-normal {
                    color: #666666;
                }
            }
        }

        .swiper-one {
            // #ifndef MP-ALIPAY
            height: #{92rpx};
            // #endif
        }
    }

    .goods-rule {
        border-bottom: #{1px} solid #e2e2e2;
        color: #353535;
        background: #FFFFFF;
        font-size: #{32rpx};
        line-height: #{80rpx};
        padding-left: #{24rpx};
        margin-top: #{20rpx};
    }

    .rule-info {
        background: #FFFFFF;
        padding-top: #{24rpx};
    }

    .bottom {
        width: 100%;
        height: #{100rpx};
        background-color: #fff;
        color: #fff;
        border-top: #{1rpx} solid #e2e2e2;
    }

    .bottom-first {
        color: #ff4544;
        background: #fff5f5;
        width: 100%;
        height: 100%;
    }

    .bottom-second {
        color: #ffffff;
        background: #ff4544;
        height: 100%;
        width: 100%;
    }

    .bottom-end {
        color: #ffffff;
        background: #cdcdcd;
        height: 100%;
        width: 100%;
    }

    .animation-model {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.25);


        .boxbg {
            height: #{840rpx};
            width: 100%;
        }

        .detaill {
            width: 100%;
            position: absolute;
            top: 50% - #{157rpx};
            z-index: 20;

            .ani_box {
                height: 0;
                width: 0;
                image {
                    opacity: 1;
                    height: 100%;
                    width: 100%;
                    z-index: 20;
                }
            }
        }

        .image {
            width: 100%;
            height: #{840rpx};
            position: absolute;
            top: #{140rpx};
            left: 0;
        }
    }
</style>